package org.duiduo.empty;

import java.util.Arrays;

/**
 * 选择排序
 * 复杂度O(n^2)
 *
 * @author liangze
 * @date 2024/2/20 11:26
 */
public class SelectSort {

    public static void select(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            int maxIndex = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[maxIndex]) {
                    maxIndex = j;
                }
            }
            if (i != maxIndex) {
                int temp = arr[i];
                arr[i] = arr[maxIndex];
                arr[maxIndex] = temp;
            }
        }
    }

    public static void main(String[] args) {
        int[] arr = {3, 6, 2, 1, 5, 4};
        select(arr);
        System.out.println(Arrays.toString(arr));
    }
}
